#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include<SDL/SDL_mixer.h>

int main()
{
    SDL_Event event ;
    int continuer = 1,imageactu=0, indice=0, gameState=0;
    int l=0 ;
    SDL_Surface *ecran = NULL,*image_new_game = NULL,*image_load_game = NULL , *image_new_game_s = NULL,*image_load_game_s = NULL ,      *image_setting = NULL, *image_setting_s = NULL , *image_quit = NULL , *image_quit_s = NULL ,*image_bateau = NULL ,*image_full_screen=NULL,
                         *image_full_screen_s=NULL,*image_return=NULL,*image_return_s=NULL,*image_muet=NULL,*image_muet_s=NULL,
                                 *image_muet1=NULL,*image_muet_s1=NULL;
     
    SDL_Rect position_new_game,position_new_game_s,position_load_game,position_load_game_s,position_setting,position_setting_s
    ,position_quit,position_quit_s,position_image_bateau,position_muet_s,position_muet,position_full_screen,position_full_screen_s,
    position_return,position_return_s,position_muet_s1,position_muet1;
 
    Mix_AllocateChannels(3);
    Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,MIX_DEFAULT_CHANNELS,1024);
    Mix_Chunk *musique,*musique1;
    musique1=Mix_LoadWAV("2041.ogg");
    musique=Mix_LoadWAV("7873.ogg");
    Mix_PlayChannel(1,musique,-1);
    Mix_Volume(1, MIX_MAX_VOLUME/2);
 
 
 
    position_image_bateau.x = 0;
    position_image_bateau.y = 0;
 
    position_new_game.x = 50;
    position_new_game.y = 100;
    position_new_game_s.x = 50;
    position_new_game_s.y = 100;
    position_load_game.x=50;
    position_load_game.y=160;
    position_load_game_s.x=50;
    position_load_game_s.y=160;
    position_setting.x=50;
    position_setting.y=220;
    position_setting_s.x=50;
    position_setting_s.y=220;
    position_quit.x = 50;
    position_quit.y = 280;
    position_quit_s.x=50;
    position_quit_s.y=280;
    position_muet_s.x=50;
    position_muet_s.y=160;
    position_muet.x=50;
    position_muet.y=160;
    position_muet_s1.x=300;
    position_muet_s1.y=160;
    position_muet1.x=300;
    position_muet1.y=160;
    position_full_screen.x=50;
    position_full_screen.y=220;
    position_full_screen_s.x=50;
    position_full_screen_s.y=220;
    position_return.x=50;
    position_return.y=100;
    position_return_s.x=50;
    position_return_s.y=100;
 
 
 
    SDL_Init(SDL_INIT_VIDEO);
    const SDL_VideoInfo* myPointer = SDL_GetVideoInfo();
    ecran=SDL_SetVideoMode(myPointer->current_w, myPointer->current_h, myPointer->vfmt->BitsPerPixel,SDL_DOUBLEBUF|SDL_FULLSCREEN);
 
 
 
 
    SDL_WM_SetCaption("5_keys", NULL);
    SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
 
    image_new_game= IMG_Load("new_game.png");
    image_new_game_s = IMG_Load("new_game_s.png");
    image_load_game = IMG_Load("load_game.png");
    image_load_game_s = IMG_Load("load_game_s.png");
    image_setting=IMG_Load("setting.png");
    image_setting_s = IMG_Load("setting_s.png");
    image_quit=IMG_Load("quit.png");
    image_quit_s = IMG_Load("quit_s.png");
    image_bateau= IMG_Load("bateau.jpg");
    image_return=IMG_Load("return.png");
    image_return_s=IMG_Load("return_s.png");
    //image_full_screen=IMG_Load("full_screen.png");
    //image_full_screen_s=IMG_Load("full_screen_s.png");
    image_muet=IMG_Load("sound_off.png");
    image_muet_s=IMG_Load("sound_off_s.png");
    image_muet1=IMG_Load("sound_on.png");
    image_muet_s1=IMG_Load("sound_on_s.png");
// blit //
 
 
 
//SDL_BlitSurface(image_new_game, NULL, ecran, &position_new_game_s);
//SDL_BlitSurface(image_load_game, NULL, ecran, &position_load_game_s);
//SDL_BlitSurface(image_setting, NULL, ecran, &position_setting_s);
//SDL_BlitSurface(image_quit, NULL, ecran, &position_quit_s);
 
    while (continuer)
    {
        SDL_PollEvent(&event);
        switch(event.type)
        {
        case SDL_QUIT:
            continuer=0;
            break;
        case SDL_MOUSEMOTION:
 
            if(gameState==0)
            {
                if ((event.motion.x<=250)&&(event.motion.x>=50)&&(event.motion.y>=140)&&(event.motion.y<=180))
                {
                    
                    indice=1;
                }
 
                else if ((event.motion.x<=250)&&(event.motion.x>=50)&&(event.motion.y>=200)&&(event.motion.y<=220))
 
                    indice=2;
 
                else if ((event.motion.x<=250)&&(event.motion.x>=50)&&(event.motion.y>=240)&&(event.motion.y<=280))
 
                    indice=3;
 
                else if ((event.motion.x<=250)&&(event.motion.x>=50)&&(event.motion.y>=320)&&(event.motion.y<=360))
 
                    indice=4;
 
 
 
 
 
                else
                {
                    Mix_HaltChannel(2);
                    indice=0;
                }
            }
 
            else if(gameState==1)
            {
                if ((event.motion.x<=250)&&(event.motion.x>=50)&&(event.motion.y>=140)&&(event.motion.y<=160))
 
                    indice=5;
                else if ((event.motion.x<=250)&&(event.motion.x>=50)&&(event.motion.y>=200)&&(event.motion.y<=240))
 
                    indice=6;
                else if ((event.motion.x<=450)&&(event.motion.x>=300)&&(event.motion.y>=200)&&(event.motion.y<=240))
 
                    indice=8;
 
                else if ((event.motion.x<=250)&&(event.motion.x>=50)&&(event.motion.y>=240)&&(event.motion.y<=280))
 
                    indice=7;
                else indice=0;
            }
 
 
 
            break;
 
 
        case SDL_MOUSEBUTTONDOWN:
            switch (event.button.button)
            {
            case SDL_BUTTON_LEFT:
                if(gameState==0)
                {
                    if ((event.motion.x<=250)&&(event.motion.x>=50)&&(event.motion.y>=140)&&(event.motion.y<=160))
 
                    {Mix_PlayChannel(2,musique1,0);
                    Mix_Volume(2, MIX_MAX_VOLUME/2);
                        //lancement du jeu
 
                    }
                    else if ((event.motion.x<=250)&&(event.motion.x>=50)&&(event.motion.y>=200)&&(event.motion.y<=220))
                    {Mix_PlayChannel(2,musique1,0);
                    Mix_Volume(2, MIX_MAX_VOLUME/2);
                        // load game
 
                    }
 
                    else if ((event.motion.x<=250)&&(event.motion.x>=50)&&(event.motion.y>=320)&&(event.motion.y<=340))
                    {Mix_PlayChannel(2,musique1,0);
                    Mix_Volume(2, MIX_MAX_VOLUME/2);
                        continuer=0 ;
                       
                    }
                    else if ((event.motion.x<=250)&&(event.motion.x>=50)&&(event.motion.y>=260)&&(event.motion.y<=280))
                    {Mix_PlayChannel(2,musique1,0);
                    Mix_Volume(2, MIX_MAX_VOLUME/2);
                        gameState=1;
                    }
                }
                else if (gameState==1)
                {
                    if ((event.motion.x<=250)&&(event.motion.x>=50)&&(event.motion.y>=140)&&(event.motion.y<=160))
 
                    {Mix_PlayChannel(2,musique1,0);
                    Mix_Volume(2, MIX_MAX_VOLUME/2);
 
                        (gameState=0);
 
                    }
                    else if ((event.motion.x<=250)&&(event.motion.x>=50)&&(event.motion.y>=200)&&(event.motion.y<=220))
                    {Mix_PlayChannel(2,musique1,0);
                    Mix_Volume(2, MIX_MAX_VOLUME/2);
                 Mix_Pause(1);
                       
                    }
                    else if ((event.motion.x<=450)&&(event.motion.x>=300)&&(event.motion.y>=200)&&(event.motion.y<=240))
                    {Mix_PlayChannel(2,musique1,0);
                    Mix_Volume(2, MIX_MAX_VOLUME/2);
                    Mix_Resume(-1);
                        
 
                    }
 
 
 
 
 
 
 
 
 
                }
 
                break;
            }
            break;
        }
 
 
        SDL_BlitSurface(image_bateau, NULL, ecran, &position_image_bateau);
        if(gameState==0)
        {
            if (indice==1)
 
                SDL_BlitSurface(image_new_game_s,NULL,ecran,&position_new_game);
            else
 
                SDL_BlitSurface(image_new_game,NULL,ecran,&position_new_game);
            if (indice==2)
 
                SDL_BlitSurface(image_load_game_s,NULL,ecran,&position_load_game);
            else
                SDL_BlitSurface(image_load_game,NULL,ecran,&position_load_game);
            if (indice==3)
 
                SDL_BlitSurface(image_setting_s,NULL,ecran,&position_setting);
            else
 
                SDL_BlitSurface(image_setting,NULL,ecran,&position_setting);
 
            if (indice==4)
 
                SDL_BlitSurface(image_quit_s,NULL,ecran,&position_quit);
            else
 
                SDL_BlitSurface(image_quit,NULL,ecran,&position_quit);
 
        }
        else if(gameState==1)
        {
 
            if (indice==5)
 
                SDL_BlitSurface(image_return_s,NULL,ecran,&position_return);
            else
 
                SDL_BlitSurface(image_return,NULL,ecran,&position_return);
            if (indice==6)
 
                SDL_BlitSurface(image_muet_s,NULL,ecran,&position_muet);
            else
 
                SDL_BlitSurface(image_muet,NULL,ecran,&position_muet);
            
            if (indice==8)
 
                SDL_BlitSurface(image_muet_s1,NULL,ecran,&position_muet1);
            else
 
                SDL_BlitSurface(image_muet1,NULL,ecran,&position_muet1);
                //if (indice==7)
 
                //SDL_BlitSurface(image_full_screen_s,NULL,ecran,&position_full_screen);
 
            //else
 
                //SDL_BlitSurface(image_full_screen,NULL,ecran,&position_full_screen);
 
 
 
 
 
 
        }
 
    SDL_Flip(ecran);
 
    }
    Mix_CloseAudio();
    Mix_FreeChunk(musique);
    Mix_FreeChunk(musique1);
    SDL_FreeSurface(ecran);
    SDL_Quit();
 
    return EXIT_SUCCESS;
}
 
 
